import { Config } from '../../utils/config.js';
Page({
  data: {
    invoice_items:[
      // { taitou: "抬头", money: "多少钱", content: "内容", state:0},
      // {  taitou: "抬头", money: "多少钱", content: "内容", state: 1},
      // { taitou: "抬头", money: "多少钱", content: "内容", state: 2}
    ],
    pageNum:1,
    pageSize:10,
    pages:0,
    nodown:true,
    nodata:true
  },

  stopLoadMoreTiem:false,// 阻止多次触发 需要的变量


  // 获取发票列表数据
  get_invoicelist(pageNo){
    let that=this;
    that.stopLoadMoreTiem = true;
    
    // that.loading = true
    wx.request({
      url: Config.restUrl + 'invoice/getListInvoices',
      data: {
        openid: "11321313131",
        pageNum: pageNo,
        pageSize: that.data.pageSize,
        kpzt: "",
        start: "",
        end: "",
        dirverName: "",
        number: "",
        item: "",
      },
      
      method: 'POST',
      dataType: 'json',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
    
      success: function(data) {
        that.stopLoadMoreTiem = false;
        wx.hideLoading()
        console.log(data.data.data);
        let pages = data.data.data.pages //总页数
        let invoice_items = that.data.invoice_items;
        let list = data.data.data.list;
        let nodown = that.data.nodown;
        let total = data.data.data.total //总数

        if (total==0){
          that.setData({
            nodata: false,
          })
        }
     
      
        if (nodown) {
          for (let i = 0; i < list.length; i++) {
            invoice_items.push(list[i]);
   
          }
        } else {
          invoice_items = list
        };

        for (let i = 0; i < list.length; i++) {

          let money = list[i].money;
          money = (money / 100)
          list[i].money = money;
        }
        // that.setData({
         
        // })
        that.setData({
          // nodown: true,
          pages: pages,
          invoice_items: invoice_items
        })
        // console.log(that.data.invoice_items);

        // that.loading = false;
        that.invoice_state()
      },
      fail: function(res) {

      },
    
    })
  },

  //下拉刷新
  // onPullDownRefresh: function () {
  //   this.onLoad()
  // },
  upper() {
    let that=this;
    wx.showLoading({
      title: '刷新中',
    });
    that.setData({
      nodown: false,
      pageNum:1
    })

    console.log("nodown" + that.data.nodown)
    wx.showNavigationBarLoading()

    
      setTimeout(() => {
        wx.hideLoading()
        that.get_invoicelist(that.data.pageNum)
        wx.stopPullDownRefresh()
        wx.hideNavigationBarLoading();

      }, 2000)
    
    
  },


  // 上拉加载
  lower() {
    if (this.stopLoadMoreTiem) {
      return;
    }

    this.setData({
      nodown: true
    })

    // if (!this.loading && this.data.pageNum < this.data.pages) {
    if (this.data.pageNum < this.data.pages) {
      wx.showLoading({
        title: '加载中',
      });
     
      this.get_invoicelist(this.data.pageNum + 1)
      this.setData({
        pageNum: this.data.pageNum + 1,
      });
      
    }
  },

  // 判断发票状态
  invoice_state(){
    let that=this;
    let invoice_items=that.data.invoice_items;

    console.log(invoice_items)

    for (let i = 0; i < invoice_items.length; i++){
      // invoice_items[i].color=""
      if (invoice_items[i].kpzt==2){
        invoice_items[i].kpzt="已开票";
        invoice_items[i].color = "#0fd58a";
      } else if (invoice_items[i].kpzt == 1){
        invoice_items[i].kpzt = "申请中";
        invoice_items[i].color = "#217ffd";
      } else if (invoice_items[i].kpzt == 3) {
        invoice_items[i].kpzt = "开票失败";
        invoice_items[i].color = "#ff7f02";
      }
      console.log(invoice_items[i].tax_no)
      if (invoice_items[i].tax_no == undefined) {
        invoice_items[i].tax_no = "个人";
  
      } else {
        invoice_items[i].tax_no = "企业";
      
      } 
    };
    that.setData({
      invoice_items: invoice_items
    })
  },


  // 点击每个发票
  click_item: function (e) {
    console.log(e.currentTarget.dataset)

    let state = e.currentTarget.dataset.state; //开票状态
    let dzfppdf = e.currentTarget.dataset.dzfppdf;  //电子发票pdf地址

    let xsfMc = e.currentTarget.dataset.xsfmc;  //售方名称
    let money = e.currentTarget.dataset.money;  //发票金额
    let xmMc = e.currentTarget.dataset.xmmc;  //项目名称
    let title = e.currentTarget.dataset.title;  //发票抬头
    let tax_no = e.currentTarget.dataset.tax_no;   //税号
    let fpqqlsh = e.currentTarget.dataset.fpqqlsh;  //发票流水号

    console.log(e)



    if (state=="已开票"){  
      wx.navigateTo({
        url: '/pages/invoice_pic/invoice_pic?fpqqlsh=' + fpqqlsh + '&dzfppdf=' + dzfppdf
      })
    } else if (state == "申请中"){
      
    } else if (state == "开票失败") {
      console.log(xmMc)
      
      wx.navigateTo({
        url: '/pages/invoice_detail/invoice_detail?xsfmc=' + xsfMc 
        + '&money=' + money
          + '&xmmc=' + xmMc
          + '&title=' + title
          + '&tax_no=' + tax_no
          + '&fpqqlsh=' + fpqqlsh
          + '&state=' + state
      })
    }
  },

  onLoad:function(){
    let that = this;
    that.get_invoicelist(that.data.pageNum)
  }

})
